from django import forms


class BootStrapModelForm(forms.ModelForm):

    # 循环插件，添加标签
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        for name, field in self.fields.items():
            if field.widget.attrs:
                field.widget.attrs["class"] = "form-control"
                field.widget.attrs["placeholder"] = field.label
            # 去掉不需要的属性
            # if name == "User_password":
            #     continue
            # print(name, field)
            field.widget.attrs = {
                "class": "form-control",
                "placeholder": field.label,
            }

    # # 钩子方法验证数据
    #
    # from django.core.exceptions import ValidationError
    # def clean_User_email(self):
    #     txt_email = self.cleaned_data["User_email"]
    #     if txt_email.find("@") == -1:
    #         raise ValidationError("邮箱格式不正确")
    #     return txt_email
